﻿//response test format:
//status:0:ok,-1:failed
//count:
//items:

//数据源

//数据绑定

//分页

//排序

//样式
//分页样式
//列表样式

/// <reference path="jquery-1.2.6-vsdoc.js" />
/// <reference path="jquery-1.2.6.js" />

/*用法1：
var r=new JsRepeater();
r.Titles="";
r.DataFields="";
r.PageSize="";
r.PageIndex="";
r.DataSource="";//获得到的数据数组（json）
    
r.DataBind();    
  
用法2
var r=new JsReapeater(titles,dataFields,pageSize,pageIndex,DataSource);
r.DataBind();
   
用法3：
var r=new JsRepeater();
r.Titles="";
r.DataFields="";
r.PageSize="";
r.PageIndex="";
r.DataSourceURL="";//获得到的URL
r.PostData=null;
r.DataMethod="Get/Post";
r.DataBind();
*/

function JsRepeater(titles, dataFields, dataMethod, postData, dataSourceURL, dataSource, allowPaging, allowSorting) {

    //基本
    var _titles = new Array();      //标题数据{"title1","title2"}
    var _dataFields = new Array();  //绑定字段{"datafield1","datafield2"}
    var _dataMethod = "post";       //获取数据的方法 datasourceurl不为空时有效
    var _postData = new Array();    //ajax post的参数 json;
    var _dataSourceURL = "";        //获取数据的url
    var _dataSource = null;   //获取到的数据 json;
    var _allowPaging = false;
    var _allowSorting = false;
    
    

    //表格样式
    //-----------------------------------------------

    //分页
    var _recordCount = 0;
    var _pageIndex = 1;
    var _pageSize = 20;
    var _currentIndex = 1;
    //分页样式
    //-----------------------------------------------

    //排序
    var _sortFields = null; //string array
    var _currentSortField = ""; //string
    var _currentSortDirection = 0; //0:asc,1:desc


    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    var _theadTemplete = "<thead><tr>$titles</tr></thead>";
    var _thTemplete = "<th>$title</th>";
    var _thSortTemplete = '<th><a href="#" id="a_sort">$title</a></th>';

    var _tbodyTemplete = "<tbody>$items</tbody>";
    var _tdTemplete = "<td>$item</td>";
    var _tdLinkTemplete = '<th><a href="#" id="a_link" target="_blank">$title</a></th>';
    var _pagerTemplete = "";

    var _thead = "<thead><tr>$titles</tr></thead>";
    var _tbody = "";
    var _pager = "";
    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    //构造函数

    //属性****************

    //标题数组
    this.Titles = function(p) {
        if (p != null)
        { _titles = p; }
        else {
            return _titles;
        }
    };
    //数据列数组
    this.DataFields = function(p) {
        if (p != null)
        { _dataFields = p; }
        else {
            return _dataFields;
        }
    };

    //获取数据的url
    this.DataSourceURL = function(p) {
        if (p != null)
        { _dataSourceURL = p; }
        else {
            return _dataSourceURL;
        }
    };

    //获取数据的方法
    this.DataMethod = function(p) {
        if (p != null)
        { _dataMethod = p; }
        else {
            return _dataMethod;
        }
    };
    //数据源
    this.DataSource = function(p) {
        if (p != null)
        { _dataSource = p; }
        else {
            return _dataSource;
        }
    };

    //********************

    this.constructor = function() {
        // dataMethod, dataSourceURL, dataSource,allowPageing,allowSorting
        _titles = titles;
        _dataFields = dataFields;
        _dataMethod = dataMethod;
        _dataSourceURL = dataSourceURL;
        _dataSource = dataSource;
        _allowPaging = allowPaging;
        _allowSorting = allowSorting;
        _postData = postData;
    };

    var GetData = function(responseData, status) {

        //获取数据，赋值给_dataSource;
        // textStatus can be one of:
        //   "timeout"
        //   "error"
        //   "notmodified"
        //   "success"
        //   "parsererror"
        if (status == "success") {

            if (responseData.status == 1 && responseData.count > 0) {
                _dataSource = responseData.items;
                _recordCount = responseData.count;
            }
        }
        else {
            alert(status);
        }

    };

    var BuildThead = function() {

        var thString = "";
        if (_titles != null && _titles.length > 0) {

            for (var i = 0; i < _titles.length; i++) {
                thString += _thTemplete.replace("$title", _titles[i]);
                //todo:排序列
            }
            _thead = _theadTemplete.replace("$titles", thString);
        }
        else {
            alert("标题数据未提供");
        }

    };

    var BuildTbody = function() {


        var trString = "";
        for (var one in _dataSource) {
            var tdString = "";
            for (var key in _dataFields) {
                alert(items[one][key]);
                tdString += _tdTemplete.replace("$item", items[one][key]);
            }
            trString += "<tr>" + tdString + "</tr>";

        }
        _tbody = _tbodyTemplete.replace("$items", trString);
    };
    var BuildPager = function() { };

    this.SetTableStyle = function() { };
    this.SetPagerStyle = function() { };

    this.DataBind = function() {

        //get data
        if (_dataSource != null || _dataSourceURL != "") {

            if (_dataSourceURL != "") {
                if (_dataMethod == "post") {

                    $.post(_dataSourceURL, _postData, GetData, "json");

                }
                else if (_dataMethod == "get") {

                    $.getJSON(_dataSourceURL, _postData, GetData);

                }
                else {
                    //获取数据方法未指定或指定错误
                    alert("获取数据方法未指定或指定错误");

                }
            }
            // buildTable
            if (_thead == "") {
                BuildThead();
            }

            BuildTbody();
            //buildPager
            BuildPager();

            //render
            $("#list").html(_thead + _tbody);
            

        }
        else {
            // 提示未绑定数据
            alert("未绑定数据");
        }



    };

    this.PageChanged = function(pageIndex) {
        _currentIndex = pageIndex;
        this.DataBind();
    };


    this.SortChanged = function(sortFiled, sortDirection) {
        _currentSortDirection = sortDirection;
        _currentSortField = sortFiled;
        this.DataBind();
    };

};


